#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<strings.h>
#include"log.hpp"

#define BACKLOG 10
class Socket;//基于对网络套接字文件描述符的封装
using SockPtr = std::shared_ptr<Socket>;
//模板方法模式
class Socket{//基类
    public:
        virtual ~Socket() = default;
        virtual void Create_Socket() = 0;
        virtual void Set_Opt() = 0;
        virtual bool Listen() = 0;
        virtual bool Bind_Socket(int port) = 0;
        virtual int Accept(struct sockaddr *client_addr) = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &in) = 0;
        virtual int Fd() = 0;
        virtual void Close() = 0;
        void Build_Socket(int port)//端口号由上层传递
        {
            Create_Socket();
            Set_Opt();
            Bind_Socket(port);
            Listen();
        }
};

class TcpSocket : public Socket{
    public:
        TcpSocket(int sockfd = -1):_sockfd(sockfd)
        {}
        virtual ~TcpSocket()
        {}    
        virtual void Create_Socket() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if(_sockfd < 0){
                LOG(log_level::ERROR)<<"socket error";
                exit(1);
            }
            LOG(log_level::INFO)<<"create socket success "<<_sockfd;

        }
        virtual void Set_Opt() override
        {
            // 保证我们的服务器，异常断开之后，可以立即重启，不会有bind问题
            int opt = 1;
            int n  = ::setsockopt(_sockfd,SOL_SOCKET,SO_REUSEADDR, &opt,sizeof(opt));
        }

        virtual bool Bind_Socket(int port) override
        {
            if(_sockfd < 0)
                return false;
            struct sockaddr_in server_addr;
            bzero(&server_addr, sizeof(server_addr));
            server_addr.sin_family = AF_INET;
            server_addr.sin_port = htons(port);
            server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
            int ret = ::bind(_sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
            if(ret < 0){
                LOG(log_level::ERROR)<<"bind error";
                exit(1);
            }
            LOG(log_level::DEBUG)<<"bind socket success "<<_sockfd;
            return true;
        
        }
        virtual bool Listen() override
        {
            if(_sockfd < 0)
                return false;
            int ret = ::listen(_sockfd, BACKLOG);
            if(ret < 0){
                LOG(log_level::ERROR)<<"listen error";
                exit(1);
            }
            LOG(log_level::DEBUG)<<"listen socket success "<<_sockfd;
            return true;
        }
        // virtual SockPtr Accept(struct sockaddr *client_addr) override
        // {
        //     if(client_addr == nullptr)
        //         return nullptr;
        //     socklen_t client_len = sizeof(struct sockaddr_in);
        //     int connect_sockfd = ::accept(_sockfd,client_addr, &client_len);
        //     if(connect_sockfd < 0){
        //         LOG(log_level::ERROR)<<"accept error";
        //         return nullptr;
        //     }
        //     LOG(log_level::DEBUG)<<"accept socket success "<<connect_sockfd;
        //     return std::make_shared<TcpSocket>(connect_sockfd);
        // }
        virtual int Accept(struct sockaddr *client_addr) override
        {
            if(client_addr == nullptr)
                return -1;
            socklen_t client_len = sizeof(struct sockaddr_in);
            int connect_sockfd = ::accept(_sockfd,client_addr, &client_len);
            if(connect_sockfd < 0){
                LOG(log_level::ERROR)<<"accept error";
                return -1;
            }
            LOG(log_level::DEBUG)<<"accept socket success "<<connect_sockfd;
            return connect_sockfd;
        }
        virtual int Recv(std::string *out) override
        {
            char buffer[1024*8];
            int len = ::recv(_sockfd,buffer,sizeof(buffer)-1,0);
            if(len > 0){
                buffer[len] = 0;
                *out = buffer;
            }
            return len;
        }
        virtual int Send(const std::string &in) override
        {
            int len = ::send(_sockfd, in.c_str(), in.size(), 0);
            return len;
        }
        virtual int Fd() override
        {
            return _sockfd;
        }
        virtual void Close() override
        {
            if(_sockfd < 0)
                return;
            ::close(_sockfd);
        }
    private:
        int _sockfd;
};


